﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entidades.Eventos;
using Negocio.Eventos;
using System.Text;
using System.IO;

public partial class Eventos_ReporteEventos : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        btnBuscarCliente.ServerClick += new EventHandler(btnBuscarCliente_ServerClick);
        btnExportarExcel.ServerClick += new EventHandler(btnExportarExcel_ServerClick);
        if (!Page.IsPostBack)
        {
            List<EveTipoEvento> eveTipoEvento = EveTipoEventoBLL.listar();
            List<ListItem> listListItem = eveTipoEvento.Select(ev => new ListItem { Value = ev.idTipoEvento.ToString(), Text = ev.nombre }).ToList();
            selectTipoEvento.Items.Clear();
            foreach (ListItem listItem in listListItem)
                selectTipoEvento.Items.Add(listItem);
        }
    }

   
    void btnBuscarCliente_ServerClick(object sender, EventArgs e)
    {
        List<EveReserva> resultReservaList = getFiltroEvento().listar();

        foreach (EveReserva item in resultReservaList)
        {
            TableRow tr1 = new TableRow();

            TableCell tc1 = new TableCell(); //Cliente
            Label lbl1 = new Label();
            lbl1.Text = item.Gen_Cliente.nombre;
            tc1.Controls.Add(lbl1);

            TableCell tc2 = new TableCell(); //Tipo Documento
            Label lbl2 = new Label();
            lbl2.Text = item.Gen_Cliente.tipoDocumento;
            tc2.Controls.Add(lbl2);

            TableCell tc3 = new TableCell(); //Numero Documento
            Label lbl3 = new Label();
            lbl3.Text = item.Gen_Cliente.NroDocumento;
            tc3.Controls.Add(lbl3);

            TableCell tc4 = new TableCell(); //Fecha Realizacion
            Label lbl4 = new Label();
            lbl4.Text = item.fechaRealizacion.Value.ToString("dd/MM/yyyy");//.toString("dd-MM-yyyy");
            tc4.Controls.Add(lbl4);

            TableCell tc5 = new TableCell(); //Monto Reserva
            Label lbl5 = new Label();
            lbl5.Text = item.totalMonto.Value.ToString("0.00");
            tc5.Controls.Add(lbl5);

            TableCell tc6 = new TableCell(); //Tipo Evento
            Label lbl6 = new Label();
            lbl6.Text = item.Eve_Evento.Eve_Tipo_evento.nombre;
            tc6.Controls.Add(lbl6);

            TableCell tc7 = new TableCell(); //Evento
            Label lbl7 = new Label();
            lbl7.Text = item.Eve_Evento.nombre;
            tc7.Controls.Add(lbl7);

            TableCell tc8 = new TableCell(); //Estado
            Label lbl8 = new Label();
            lbl8.Text = ConstantesEventos.getDescripcionEstadoReserva(item.estado.Value);    
            tc8.Controls.Add(lbl8);

            tr1.Cells.Add(tc1);
            tr1.Cells.Add(tc2);
            tr1.Cells.Add(tc3);
            tr1.Cells.Add(tc4);
            tr1.Cells.Add(tc5);
            tr1.Cells.Add(tc6);
            tr1.Cells.Add(tc7);
            tr1.Cells.Add(tc8);

            tableEventos.Rows.Add(tr1);
        }
    }

    private EveFiltroReserva getFiltroEvento()
    {
        return new EveFiltroReserva
        {
            Cliente = txtCliente.Value,
            TipoDocumento = selectTipoDocumento.Value,
            NumeroDocumento = txtNumeroDocumento.Value,
            FechaRealizacionInicio = txtFechaInicio.Value!=""? Convert.ToDateTime(txtFechaInicio.Value):DateTime.Now.AddYears(-100),
            FechaRealizacionFin = txtFechaFin.Value != "" ? Convert.ToDateTime(txtFechaFin.Value) : DateTime.Now.AddYears(100),
            MontoReserva = Convert.ToDecimal(txtMonto.Value==string.Empty?"0":txtMonto.Value),
            TipoMontoReserva = Convert.ToInt32(selectTipoMontoEvento.Value),
            TipoEvento = selectTipoEvento.Value,
            NombreEvento = txtNombreEvento.Value,
            Estado = Convert.ToInt32(selectEstado.Value)
        };
    }

    #region Exportar
    void btnExportarExcel_ServerClick(object sender, EventArgs e)
    {

        List<EveReserva> resultReservaList = getFiltroEvento().listar();

        string attachment = "attachment; filename=ReporteEventos.xls";
        Response.ClearContent();
        Response.Charset = "UTF-8";
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/vnd.ms-excel";
        Response.ContentEncoding = Encoding.Default;

        System.Web.UI.Page page1 = new System.Web.UI.Page();
        StringBuilder builder1 = new StringBuilder();
        StringWriter writer1 = new StringWriter(builder1);
        HtmlTextWriter writer2 = new HtmlTextWriter(writer1);
        HttpContext context = HttpContext.Current;
        string cuerpoExcel = string.Empty;

        /*cuerpoExcel = @"<H3><div align='center'><br><br>REPORTE EVENTOS</div></H3> <br>
                        <table class='exampletable' border='1' cellpadding='5' cellspacing='5'>";*/
        cuerpoExcel = @"<table class='exampletable' border='1' cellpadding='5' cellspacing='5'>";
        string columnas = @"
                            <tr>
                            <th width='200' style='background-color:#CE8E15'><div align='center'><font color ='#202020'> CLIENTE</font></div></th>    
                            <th width='150' style='background-color:#CE8E15'><div align='center'><font color ='#202020'>TIPO DOCUMENTO</font></div></th>
                            <th width='130' style='background-color:#CE8E15'><div align='center'><font color ='#202020'>NUMERO DOCUMENTO</font></div></th>    
                         
                            <th width='130' style='background-color:#CE8E15'><div align='center'><font color ='#202020'>FECHA REALIZACION</font></div></th>
                            <th width='130' style='background-color:#CE8E15'><div align='center'><font color ='#202020'>MONTO RESERVA (S/.)</font></div></th>
                            <th width='120' style='background-color:#CE8E15'><div align='center'><font color ='#202020'>TIPO EVENTO</font></div></th>
                            <th width='120' style='background-color:#CE8E15'><div align='center'><font color ='#202020'>EVENTO</font></div></th>
                            <th width='100' style='background-color:#CE8E15'><div align='center'><font color ='#202020'>ESTADO</font></div></th>
                            
                            </tr>
                          ";
        cuerpoExcel = cuerpoExcel + columnas;

            foreach (EveReserva item in resultReservaList)
            {
                // .ToString("dd-MM-yyyy")
                   string cuerpo = @"<tr >
                        <td  width='200'><div align='center'>" + item.Gen_Cliente.nombre + @"</div></td>
                        <td  width='150'><div align='center'>" + item.Gen_Cliente.tipoDocumento + @"</div></td>
                        <td  width='150'><div align='center'>" + item.Gen_Cliente.NroDocumento + @"</div></td>
                          
                        <td  width='60'><div align='center'>" + item.fechaRealizacion.Value.ToString("dd/MM/yyyy") + @"</div></td>
                        <td  width='60'><div align='center'>" + item.totalMonto.Value.ToString("0.00") + @"</div></td>
                        <td  width='60'><div align='center'>" + item.Eve_Evento.Eve_Tipo_evento.nombre + @"</div></td>
                        <td  width='60'><div align='center'>" + item.Eve_Evento.nombre + @"</div></td>
                        <td  width='60'><div align='center'>" + item.estado + @"</div></td>
                      </tr>";
                cuerpoExcel = cuerpoExcel + cuerpo;
            }

        cuerpoExcel = cuerpoExcel + "</table><p>.</p>";
        writer2.Write(cuerpoExcel);
        page1.DesignerInitialize();
        page1.RenderControl(writer2);
        page1.Dispose();
        page1 = null;
        Response.Write(builder1.ToString());
        Response.End();
    }
    #endregion


}